using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using XProject.DAO;
using XProject.Entity;
namespace XProject.GUI
{
    public partial class Registration : Form
    {
        PersonDAO daoPerson = new PersonDAO();
        AccountDAO daoAccount = new AccountDAO();
        public Registration()
        {
            InitializeComponent();
        }

        private void chkGender_CheckedChanged(object sender, EventArgs e)
        {
            if (chkGender.Checked) chkGender.Text = "Male";
            else chkGender.Text = "Female";
        }

        private void Registration_Load(object sender, EventArgs e)
        {
            //set icon
            //string icon = Application.StartupPath + "\\" + ConfigurationManager.AppSettings["icon"];
            //if (System.IO.File.Exists(icon)) { this.Icon = new Icon(icon); }
            //load data
            cmbRole.Items.Add(new Util.ListItem("Administrator",1));
            cmbRole.Items.Add(new Util.ListItem("User", 2));
            cmbRole.SelectedIndex = 1;
        }

        private void btnCreateAccount_Click(object sender, EventArgs e)
        {
            if (!IsValidInput())
            {
                MessageBox.Show("Please fill all required fields and make sure that password and confirm password are the same");
                return;
            }
            Person person = new Person();
            Account account = new Account();
            person.Name = txtFullname.Text;
            person.DateOfBirth = dtpDateOfBirth.Value;
            person.Gender = chkGender.Checked;
            person.Contact.Address = txtAddress.Text;            
            account.Username = txtUsername.Text;
            account.Password = Util.Encryptor.MD5Encrypt(txtPassword.Text);
            Util.ListItem item = cmbRole.SelectedItem as Util.ListItem;
            int role = int.Parse(item.Tag.ToString());
            account.Role = role;
            account.Person = person;
            if (daoPerson.Insert(person) <= 0)
            {
                MessageBox.Show(daoPerson.Message,"Error");
                txtUsername.Text = "";
                txtPassword.Text = "";
                txtConfirmPass.Text = "";
                txtFullname.Text = "";
                txtAddress.Text = "";
                return;
            }
            if (daoAccount.Insert(account) <= 0)
            {
                MessageBox.Show(daoAccount.Message, "Error");
            }
            else
            {
                MessageBox.Show("New account has been created successfully");
                this.Dispose();
            }
        }
        bool IsValidInput()
        {
            if (txtUsername.Text == "") return false;
            if (txtPassword.Text == "") return false;
            if (txtConfirmPass.Text != txtPassword.Text) return false;
            if (txtFullname.Text == "") return false;
            return true;
        }
    }
}